package com.trans.entity;

import com.trans.anno.Info;
import com.trans.anno.RemoteRequest;
import com.trans.interf.ResultHandle;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.core5.http.HttpRequestInterceptor;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author xwduo
 * @Date 2024-01-19 16:50
 */
@Slf4j
@AllArgsConstructor
public class RequestInfo {
    public final String url;
    public final String methodName;
    public final Class<? extends ResultHandle> resultHandle;
    public final String paramsName;
    public final List<HttpRequestInterceptor> interceptorList;

    public RequestInfo(RemoteRequest request) {
        this.url = request.url();
        this.methodName = request.methodName();
        this.resultHandle = request.resultHandle();
        this.paramsName = request.paramsName();
        this.interceptorList =  Arrays.stream(request.interceptor()).map(clazz ->{
                    try {
                        return clazz.newInstance();
                    }catch (Exception e){
                        log.error("类：{} 无法实例化",clazz.getName());
                        return null;
                    }
                }).filter(Objects::nonNull).collect(Collectors.toList());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        };
        if (o == null || getClass() != o.getClass()) {
            return false;
        };
        RequestInfo info = (RequestInfo) o;
        return Objects.equals(url, info.url) && Objects.equals(methodName, info.methodName) && Objects.equals(resultHandle, info.resultHandle) && Objects.equals(paramsName, info.paramsName)
                &&  Objects.equals(interceptorList, info.interceptorList);
    }

    @Override
    public int hashCode() {
        return Objects.hash(url, methodName, resultHandle, paramsName, interceptorList);
    }
}
